package module.index;

import com.solr43.bean.BoryouBean;
import com.solr43.index.IndexBizImpl;
import com.solr43.index.IndexSolrServer;
import com.solr43.util.MD5Sign;
import com.solr43.util.Sign;
import entity.NeedFetchUrl;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.jsoup.helper.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

/**
 * @fileName: IndexSolr
 * @author: Han
 * @date: 2018/6/11 9:55
 * @dscription: TODO
 */
public class IndexSolr {
    /**
     * 创建单例实例
     */
    private static final IndexSolr INDEX_SOLR = new IndexSolr();
    /**
     * 获取server的单实例
     */
    private IndexSolrServer indexSolrServer = IndexSolrServer.getIndexSolrServerInstance();
    /**
     * 获取索引添加实例
     */
    private IndexBizImpl indexBiz = new IndexBizImpl();

    public static IndexSolr getIndexSolrInstance() {
        return INDEX_SOLR;
    }

    private BoryouBean needFetchUrlToBoryouBean(NeedFetchUrl needFetchUrl) {
        BoryouBean boryouBean = new BoryouBean();
        //设置id
        boryouBean.setId(Sign.getMD5(needFetchUrl.getUrl() + needFetchUrl.getType()) + "");
        //设置标题
        boryouBean.setTitle(needFetchUrl.getTitle());
        if (needFetchUrl.getTitle().contains("转载")) {
            boryouBean.setIsHead(1);
        }
        //设置时间
        boryouBean.setTime(needFetchUrl.getTime());
        //设置日期
        boryouBean.setTimeIndex(needFetchUrl.getTime().substring(0, 8));
        //设置正文
        boryouBean.setText(needFetchUrl.getContent());
        //设置为舆情通主页
        boryouBean.setSector("www.yqt365.com");
        //设置文章链接地址
        boryouBean.setUrl(needFetchUrl.getUrl());
        //设置md5
        boryouBean.setMD5(MD5Sign.getMD5Sign(needFetchUrl.getTitle(), false, false,
                false, false, false));
        //设置文章类型,0:论坛,1:新闻,2:博客,3:微博
        boryouBean.setType(needFetchUrl.getType());
        //设置发送索引时间
        boryouBean.setPostTime(System.currentTimeMillis());
        //设置语言表示,0:中文,1:英文
        boryouBean.setLanguage_i(0);
        //设置来源
        boryouBean.setTextSource_t(needFetchUrl.getSource());

        return boryouBean;
    }

    private CloudSolrServer getCloudSolrServer(BoryouBean bean) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        CloudSolrServer cloudSolrServer = null;
        try {
            cloudSolrServer = indexSolrServer.getServerByTime(format.parse(bean.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return cloudSolrServer;
    }

    public boolean sendToIndex(NeedFetchUrl needFetchUrl) {
        //表示索引添加是否成功
        boolean flag = false;
        //转换needFetchUrl为索引bean字段
        BoryouBean boryouBean = needFetchUrlToBoryouBean(needFetchUrl);
        try {
            //添加索引
            if (!Objects.isNull(boryouBean) && !StringUtil.isBlank(boryouBean.getTime())) {
                CloudSolrServer solrServer = getCloudSolrServer(boryouBean);
                if (!Objects.isNull(solrServer)) {
                    flag = indexBiz.boryouAdd(boryouBean, getCloudSolrServer(boryouBean));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }
}
